package br.com.triad.testelucasdsniel;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class CadastroActivity extends Activity {

	Evento ev = new Evento();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastro);
		
		String latlon = getIntent().getExtras().getString("latlon");
		final TextView txtEndereco = (TextView) findViewById(R.activity_cadastro.txtEndereco);
		txtEndereco.setText(latlon);
		ev.setEndereco(latlon);
		
		EditText edtNome = (EditText) findViewById(R.activity_cadastro.edtNome);
		edtNome.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (count > 0) {
					ev.setNome(s.toString());
				}
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			public void afterTextChanged(Editable s) {
			}
		});
		
		EditText edtValor = (EditText) findViewById(R.activity_cadastro.edtValor);
		edtValor.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (count > 0) {
					ev.setValor(Integer.valueOf(s.toString()));
				}
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			public void afterTextChanged(Editable s) {
			}
		});
		
		final TextView txtData = (TextView) findViewById(R.activity_cadastro.txt_data);
		
		final Calendar c = Calendar.getInstance();
		Button btnData = (Button) findViewById(R.activity_cadastro.btn_data);
		btnData.setOnClickListener(new View.OnClickListener() {
			// @Override
			public void onClick(View arg0) {
				final DatePickerDialog dpd = new DatePickerDialog(CadastroActivity.this, new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
						txtData.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
						c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						c.set(Calendar.MONTH, monthOfYear);
						c.set(Calendar.YEAR, year);
						ev.setDataEvento(c.getTime());
					}
				}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
				dpd.show();
			}
		});
		
		Button btnSalvar = (Button) findViewById(R.activity_cadastro.button1);
		btnSalvar.setOnClickListener(new View.OnClickListener() {
			// @Override
			public void onClick(View arg0) {
				// TODO validar campos sem preenchimento aqui...
				// eu poderia criar varias camadas para controlar, mas nao tenho tempo ;)

				DatabaseHelper dbHelper = new DatabaseHelper(CadastroActivity.this);
				SQLiteDatabase dbase = dbHelper.getWritableDatabase();
				try {
					dbase.beginTransaction();
					ContentValues cv = new ContentValues();
					cv.put(NOME, ev.getNome());
					cv.put(VALOR, ev.getValor());
					if (ev.getDataEvento() != null) {
						cv.put(DATA_EVENTO, ev.getDataEvento().getTime());
					}
					cv.put(ENDERECO, ev.getEndereco());

					dbase.insert(TABLE, NOME, cv);
					dbase.setTransactionSuccessful();
				} finally {
					dbase.endTransaction();
					dbase.close();
				}
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("create table " + TABLE + " (" + ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + NOME + " text null, " + VALOR + " NUMBER NULL," + DATA_EVENTO + " text null, "
					+ ENDERECO + " text null " + ");");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// db.execSQL("DROP TABLE IF EXIST " + TABLE);
			onCreate(db);
		}
	}

	private static final String DATABASE_NAME = "triad.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE = "eventos";
	private static final String ID = "id";
	private static final String NOME = "nome";
	private static final String VALOR = "valor";
	private static final String DATA_EVENTO = "dataevento";
	private static final String ENDERECO = "endereco";
}
